﻿<?php
class SearchAction extends Action{
    public function index(){
    	if($_GET["kw"]){	
    		if($_GET["action-search-md"]){
    			$type='1';
    		}else if($_GET["action-search-pf"]){
    			$type='2';
    		} else if($_GET["action-search-fw"]){
    			$type='3';
    		}else{
    			$this->redirect("Index/index");
    		}
			$this->search(trim($_GET["kw"]),$type);
			$keyword=urldecode($_GET["kw"]);
			
		}else{
			$this->redirect("Index/index");
		}
    }
    
	public function search($keyword,$type){
		import("@.ORG.Page");
		$_SERVER['REQUEST_URI'] = mb_convert_encoding($_SERVER['REQUEST_URI'], "UTF-8","GBK");
		$storeModel = M('store');
		$keyword = urldecode($keyword);
		//$keyword = mb_convert_encoding($keyword, "UTF-8","GBK"); 
		$seriesStr = new series();
		
		if($type){
			$whereStr = "store_type = '".$type."' and (store_name like '%".$keyword."%' or store_address like '%".$keyword."%' or store_id like '%".$keyword."%') ";
		}else{
			$whereStr = "store_name like '%".$keyword."%' or store_address like '%".$keyword."%' or store_id like '%".$keyword."%' ";
		}
		
		$seriessWhereStr="";
		foreach($seriesStr->seriesStr as $key => $value){
			$seriessWhereStr.="OR (series_".$value." LIKE '%".$keyword."%' AND series_".$value."_show = '0' )";
		}
		
		
		
		$count = $storeModel->join("suser ON suser.suser_id = store.store_mid")
				->join("series ON  store.store_id = series.series_sid")
				->where($whereStr.$seriessWhereStr)
				->count();
		$p = new Page($count,10);
		
		$list = $storeModel->distinct(true)
				->join("suser ON suser.suser_id = store.store_mid")
				->join("series ON  store.store_id = series.series_sid and series_mid=''")
				->where($whereStr.$seriessWhereStr)
				->limit($p->firstRow.','.$p->listRows)
				->findAll();
		if(!$type){
			$type = $list[0]['store_type'];
		}
		$page = $p->show();
		$this->assign("list",$list);
		$this->assign("page", $page); 
		$this->assign('count',$count);
		$this->assign("kw", $keyword);
		$this->assign("type", $type);
		$this->display("search");
		
	}
}

class series{	
	public $seriesStr=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
}
?>